#include<stdio.h>
#include<stdlib.h>
typedef struct PolyNode *Polynomial;
struct PolyNode {
    int coef;
    int expon;
    Polynomial Link;
};
typedef Polynomial pol;
void Attach(int c, int e, pol *PRear)
{
    pol P;
    P = (pol)malloc(sizeof(struct PolyNode));
    P->coef = c;
    P->expon = e;
    P->Link = NULL;
    (*PRear)->Link = P;
    *PRear = P;
}
pol ReadPoly()
{
    int c, e;
    char ch = '\0';
    pol P, Rear, t;
    P = (pol)malloc(sizeof(struct PolyNode));
    P->Link = NULL;
    Rear = P;
    while(ch!='\n')
    {
        scanf("%d%d", &c, &e);
        ch = getchar();
        Attach(c, e, &Rear);
    }  
    t = P; P = P->Link; free(t);
    return P;
}
pol SumPoly(pol P1, pol P2)
{
    pol P, Rear, t1 = P1, t2 = P2;
    P = (pol)malloc(sizeof(struct PolyNode));
    P->Link = NULL;
    Rear = P;
    while(t1 && t2){
        if(t1->expon == t2->expon){
            int c = t1->coef + t2->coef;
            if(c){
                int e = t1->expon;
                Attach(c, e, &Rear);
            }
            t1 = t1->Link; t2 = t2->Link;
        }
        else if(t1->expon > t2->expon){
            Rear->Link = t1;
            Rear = t1;
            t1 = t1->Link;
        }
        else{
            Rear->Link = t2;
            Rear = t2;
            t2 = t2->Link;
        }
    }
    while(t1){
        if(t1->coef){
            Rear->Link = t1;
            Rear = t1;            
        }
        t1 = t1->Link;
    }
    while(t2){
        if(t2->coef){
            Rear->Link = t2;
            Rear = t2;            
        }
        t2 = t2->Link;
    }
    pol t = P; P = P->Link; free(t);
    return P;
}
pol MultiPoly(pol P1, pol P2)
{
    if(!P1 || !P2) return NULL;
    pol P, p, Rear, rear, t1 = P1, t2 = P2;
    P = (pol)malloc(sizeof(struct PolyNode));
    P->Link = NULL; P->coef = 0; P->expon = 0;
    Rear = P;
    while(t1){
        p = (pol)malloc(sizeof(struct PolyNode));
        p->Link = NULL;
        rear = p;
        while(t2){
            Attach(t1->coef*t2->coef, t1->expon + t2->expon, &rear);
            t2 = t2->Link;
        }
        pol t = p; p = p->Link; free(t);
        P = SumPoly(P, p);
        t1 = t1->Link; t2 = P2;
    }
    return P;
}

void PrintPoly(pol P)
{
    int flag = 0;
    while(P){
        if(!flag) flag = 1;
        else printf(" ");
        printf("%d %d", P->coef, P->expon);
        P = P->Link;
    }
    printf("\n");
}
int main()
{
    pol P1, P2;
    P1 = ReadPoly();
    P2 = ReadPoly();
    pol ans = MultiPoly(P1, P2);
    PrintPoly(ans);
    return 0;
}